/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.migracion.dataaccess;

import com.migracion.components.Column;
import com.migracion.components.DataType;
import com.migracion.components.util.StatementRequest;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.antlr.stringtemplate.StringTemplate;

/**
 *
 * @author stevenziggiz
 */
public class DataAccess {

    public boolean schemaExist(StatementRequest statementRequest) throws ClassNotFoundException, SQLException, Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<String> getTablesNameList(StatementRequest statementRequest) throws ClassNotFoundException, SQLException, Exception {
        List<String> tablesNameList = new ArrayList<String>();
        ResultSet rs;
        Statement st;
        st = statementRequest.getDriverConnectionPool().conectar().createStatement();
        rs = st.executeQuery(statementRequest.getDriverConnectionPool().getTablesList());
        while (rs.next()) {
            tablesNameList.add(rs.getString(1));
        }
        st.close();
        rs.close();
        return tablesNameList;
    }

    public List<Column> getColumnsNameList(StatementRequest statementRequest) throws ClassNotFoundException, SQLException, Exception {
        List<Column> columnsNameList = new ArrayList<Column>();
        ResultSet rs;
        Statement st;
        int index = 0;
        Column column;
        boolean white;
        DataType dataType;
        StringTemplate stringTemplate = new StringTemplate(statementRequest.getDriverConnectionPool().getColumnsList());
        stringTemplate.setAttribute("TABLE_PARAM", statementRequest.getTableName());
        st = statementRequest.getDriverConnectionPool().conectar().createStatement();
        rs = st.executeQuery(stringTemplate.toString());
        while (rs.next()) {
            if (rs.getString(3).equals("YES") || rs.getString(3).equals("Y")) {
                white = true;
            } else {
                white = false;
            }
            dataType = DataType.valueOf(rs.getString(2).split("\\(")[0].toUpperCase());
            if (dataType != null) {
                column = new Column(index, rs.getString(1), dataType, white,null);
                columnsNameList.add(column);
                index++;
            } else {
                throw new Exception("Tipo de dato no soportado: " + rs.getString(2).toUpperCase());
            }
        }
        st.close();
        rs.close();
        return columnsNameList;
    }

    public boolean columnTypeVeryfication(StatementRequest statementRequest, String tableName, String columnName, String type) throws ClassNotFoundException, SQLException, Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean columnNullVeryfication(StatementRequest statementRequest, String tableName, String columnName) throws ClassNotFoundException, SQLException, Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public static List<StringInsert> getDataMigration(StatementRequest statementRequest, String query, int columns) throws ClassNotFoundException, SQLException, Exception {
        List<StringInsert> dataResult = new ArrayList<StringInsert>();
        ResultSet rs;
        Statement st;
        StringInsert stringInsert = new StringInsert();
        st = statementRequest.getDriverConnectionPool().conectar().createStatement();
        rs = st.executeQuery(query);
        while (rs.next()) {
            for (int i = 1; i <= columns; i++) {
                stringInsert.addData(rs.getObject(i));
            }
            dataResult.add(stringInsert);
            stringInsert = new StringInsert();
        }
        st.close();
        rs.close();
        return dataResult;
    }

    public static List<String> insert(StatementRequest statementRequest, List<StringInsert> stringInsertsList, String tableName, String columns) {
        List<String> resultList = new ArrayList<String>();
        try {
            Statement st;
            st = statementRequest.getDriverConnectionPool().conectar().createStatement();
            for (StringInsert stringInsert : stringInsertsList) {
                try {
                    st.execute("Insert into "+tableName+" \\(" + columns + "\\) values\\(" + stringInsert.getInsert() + "\\)");
                } catch (SQLException ex) {
                    resultList.add("ERROR: No se pudo insertar");
                }
                resultList.add("INFO: Insercion exitosa!!");
            }
            st.close();
        } catch (SQLException ex) {
            Logger.getLogger(DataAccess.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DataAccess.class.getName()).log(Level.SEVERE, null, ex);
        }
        return resultList;
    }
}
